package com.yonyou.hxjy.service.dingtalk.Impl.toysImpl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yonyou.hxjy.entity.ProcessInstance;
import com.yonyou.hxjy.service.dingtalk.DingtalkFundAllocationToYs;
import com.yonyou.hxjy.service.dingtalk.DingtalkProcessInstanceToYs;
import com.yonyou.hxjy.service.dingtalk.Impl.YsDataGetService;
import com.yonyou.hxjy.utils.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class DingtalkFundAllocationToYsImpl implements DingtalkFundAllocationToYs {
    @Autowired
    private YsDataGetService ysDataGetService;

    @Autowired
    private DingtalkProcessInstanceToYs defaultDataService;

    private static final Logger logger = LoggerFactory.getLogger(DingtalkFundAllocationToYs.class);

    private static final Map<String, String> quickTypeMap = new HashMap<String, String>() {{
        put("内部借款", "23");
        put("集团内部利润结算","24");
        put("往来款","25");
        put("资金归集","9");
    }};

    /**
     * @param jsonObjectList
     * @return
     * 保存资金调拨订单付款单到Ys中
     */
    @Override
    public List<JSONObject> fundAllocation_toYsJson(List<JSONObject> jsonObjectList) {
//        List<JSONObject> filterReviewer = defaultDataService.filter_Reviewer(jsonObjectList);
        List<JSONObject> ysJsonResponseList = new ArrayList<>(); //返回结果列表
        Map<String, Object> dataMap;//参数汇总
        String businessId = "";
        //获取银行数据
        List<JSONObject> bankList = ysDataGetService.getBankDetailsMessage();
        //获取供应商列表
        List<JSONObject> vendorList = ysDataGetService.getVendorDetailsMessage();
        //响应结果
        JSONObject jsonObjectResponse;
        //遍历ID集合构建参数
        for (JSONObject jsonObject :jsonObjectList) {
            try {
                //获取审批实例业务编号
                businessId = jsonObject.getJSONObject("result").getStr("businessId");
                JSONArray jsonArrayTasks = jsonObject.getJSONObject("result").getJSONArray("tasks");
                //审批id对象
                ProcessInstance processInstance = defaultDataService.getProcessInstance(jsonArrayTasks);
                dataMap = new HashMap<>();
                //构建参数主表
                Map <String,Object> paramMap = new HashMap<>();
                paramMap.put("vouchdate",dealTime(jsonObject.getJSONObject("result").getStr("createTime")));//审批中时间
                paramMap.put("exchangeRateType_code",defaultDataService.EXCHANGE_RATE_TYPE_CODE); //汇率类型
                paramMap.put("tradetype_code", defaultDataService.TRADE_TYPE_CODE[2]);  //资金调拨付款单交易类型
                paramMap.put("description",businessId); //审批单据号
                paramMap.put("exchRate",defaultDataService.EXCH_RAGE);  //汇率值
                paramMap.put("currency_code",defaultDataService.CURRENCY_CODE); //币种ID
                paramMap.put("settlemode_code",defaultDataService.SETTLEMODE_CODE[0]); //结算方式
                paramMap.put("code",businessId); //单据号
                paramMap.put("_status","Insert");

                //构建资金付款单子表
                List<Map<String, Object>> fundPaymentBList = new ArrayList<>();
                Map<String, Object> fundPaymentBItem = new HashMap<>();
                fundPaymentBItem.put("quickType_code", defaultDataService.QUICK_TYPE_CODE[2]); //资金调拨付款单款项类型
                fundPaymentBItem.put("settlestatus", "1"); // 结算状态
                fundPaymentBItem.put("caobject", 2); // 付款对象(供应商)
                fundPaymentBItem.put("_status", "Insert"); // 动作
                fundPaymentBItem.put("settlemode_code",defaultDataService.SETTLEMODE_CODE[0]); //子表结算方式
                //设置单据创建时间
                processInstance.setCreate(jsonObject.getJSONObject("result").getStr("create"));
                processInstance.setUpdateDate(new Date());
                processInstance.setBusinessId(businessId);
                //保存资金收款单
                jsonObjectResponse = fundAllocation_fundCollectionBill(paramMap, fundPaymentBItem, jsonObject,bankList,vendorList);
                //处理响应结果
                if (jsonObjectResponse.getStr("code").equals("200")){
                    //保存成功，数据库中修改标记
                    JSONObject data = jsonObjectResponse.getJSONObject("data");
                    String id = data.getStr("id");
                    Map<String,Object> submit=new HashMap<>();
                    Map<String,Object> submitdata=new HashMap<>();
                    submitdata.put("id",id);
                    submit.put("data",submitdata);
                    jsonObjectResponse = defaultDataService.sendPostToSubmit(submit); //发送请求
                }                //保存收款单响应结果
                ysJsonResponseList.add(jsonObjectResponse.getJSONObject("data"));
                //对付款单进行保存
                JSONArray jsonArray = jsonObject.getJSONObject("result").getJSONArray("formComponentValues");
                //遍历组件里的value值
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject item = jsonArray.getJSONObject(i);
                    //设置金额
//                    if ("TableField_1K5PR4QRJQYO0".equals(item.getStr("id"))){
//                        JSONArray value = item.getJSONArray("value");
//                        JSONArray rowValue = value.getJSONObject(0).getJSONArray("rowValue");
//                        //检查该数据中是否包含金额value
//                        if ("调拨金额（元）".equals(rowValue.getJSONObject(1).getStr("label"))){
//                            double amount = Double.parseDouble(rowValue.getJSONObject(1).getStr("value"));
//                            fundPaymentBItem.put("oriSum",amount);
//                        }
//                        continue;
//                    }
                    if ("TableField-MINGXI".equals(item.getStr("id"))){
                        JSONArray value = item.getJSONArray("value");
                        JSONArray rowValue = value.getJSONObject(0).getJSONArray("rowValue");
                        //检查该数据中是否包含金额value
                        if ("调拨金额（元）".equals(rowValue.getJSONObject(1).getStr("label"))){
                            double amount = Double.parseDouble(rowValue.getJSONObject(1).getStr("value"));
                            fundPaymentBItem.put("oriSum",amount);
                        }
                        continue;
                    }
                    if ("DDSelectField_326QK5S77MW0".equals(item.getStr("id"))){
                        String value = item.getStr("value");
                        fundPaymentBItem.put("quickType_code",quickTypeMap.get(value));
                        continue;
                    }
                    //设置转出公司主体(付款主体)
//                    if ("TextField_9SGHDIG5KI80".equals(item.getStr("id"))){
//                        //缓存到redis中
//                        String uitCodeKey = "组织名称:" + item.getStr("value");
//                        String uitCode = (String) RedisUtils.get(uitCodeKey);
//                        //不存在发送请求获取组织编码
//                        if (StringUtils.isEmpty(uitCode)){
//                            //根据组织名称获取code
//                            uitCode = ysDataGetService.getOrgUitCode(item.getStr("value"));
//                            RedisUtils.set(uitCodeKey, uitCode);
//                        }
//                        if (uitCode!=null && !uitCode.isEmpty()){
//                            paramMap.put("accentityRaw_code",uitCode );
//                            paramMap.put("accentity_code", uitCode);
//                            paramMap.put("org_code", uitCode);
//                            //获取企业银行账号
//                            String bankCode = defaultDataService.getBankCode(item.getStr("value"),bankList);
//                            logger.info("付款公司银行账号:{}",bankCode);
//                            if (bankCode!=null && !bankCode.isEmpty()){
//                                paramMap.put("enterprisebankaccount_code",bankCode);// 企业银行账户编码
//                                fundPaymentBItem.put("enterprisebankaccount_code", bankCode); // (子表)企业银行账户编码
//                            }else {
//                                throw new RuntimeException("{error:\"获取付款单企业银行账号失败-资金调拨审批单"+businessId+"中默认银行账号不存在！！！\"}");
//                            }
//                        }else {
//                            throw new RuntimeException("{error:\"获取付款单组织code失败-资金调拨审批单"+businessId+"中付款组织不存在！！！\"}");
//                        }
//                        continue;
//                    }
                    if ("TextField_1WBABIWCT5FK0".equals(item.getStr("id"))){
                        //缓存到redis中
                        String uitCodeKey = "组织名称:" + item.getStr("value");
                        String uitCode = (String) RedisUtils.get(uitCodeKey);
                        //不存在发送请求获取组织编码
                        if (StringUtils.isEmpty(uitCode)){
                            //根据组织名称获取code
                            uitCode = ysDataGetService.getOrgUitCode(item.getStr("value"));
                            RedisUtils.set(uitCodeKey, uitCode);
                        }
                        if (uitCode!=null && !uitCode.isEmpty()){
                            paramMap.put("accentityRaw_code",uitCode );
                            paramMap.put("accentity_code", uitCode);
                            paramMap.put("org_code", uitCode);
                            //获取企业银行账号
//                            String bankCode = defaultDataService.getBankCode(item.getStr("value"),bankList);
//                            logger.info("付款公司银行账号:{}",bankCode);
//                            if (bankCode!=null && !bankCode.isEmpty()){
//                                paramMap.put("enterprisebankaccount_code",bankCode);// 企业银行账户编码
//                                fundPaymentBItem.put("enterprisebankaccount_code", bankCode); // (子表)企业银行账户编码
//                            }else {
//                                throw new RuntimeException("{error:\"获取付款单企业银行账号失败-资金调拨审批单"+businessId+"中默认银行账号不存在！！！\"}");
//                            }
                        }else {
                            throw new RuntimeException("{error:\"获取付款单组织code失败-资金调拨审批单"+businessId+"中付款组织不存在！！！\"}");
                        }
                        continue;
                    }
                    //转出公司银行账号(付款账号)
                    if ("TextField_UULPWK1H8MO0".equals(item.getStr("id"))){
                        //获取企业银行账号
                        String bankCode = item.getStr("value");
                        if (bankCode!=null && !bankCode.isEmpty()){
                            paramMap.put("enterprisebankaccount_code",bankCode);// 企业银行账户编码
                            fundPaymentBItem.put("enterprisebankaccount_code", bankCode); // (子表)企业银行账户编码
                        }else {
                            throw new RuntimeException("{error:\"获取企业银行账号失败-资金调拨审批单"+businessId+"中默认银行账号不存在！！！\"}");
                        }
                        continue;
                    }

                    //转入公司银行账号(收款账号)
//                    if ("RecipientAccountField_55KLPG330F00".equals(item.getStr("id"))){
//                        //设置收款方单位，收款方账号
//                        if (item.containsKey("extValue")){
//                            JSONObject extValueJson = item.getJSONObject("extValue");
//                            if (!extValueJson.containsKey("cardNo")) {
//                                throw new RuntimeException("{error:\"获取付款单供应商银行账号失败-资金调拨审批单" + businessId + "中供应商收款账号不存在！！！\"}");
//                            }
//                            String name = extValueJson.getStr("name");
//                            //供应商子表银行网点信息参数
//                            Map<String,Object> bankParamMap = new HashMap<>();
//                            String cardNo = extValueJson.getStr("cardNo"); //银行账号
//                            bankParamMap.put("account",extValueJson.getStr("cardNo")); //银行账号
//                            bankParamMap.put("accountname",extValueJson.getStr("name")); ////账号名称
//
//                            JSONObject instBranchName = ysDataGetService.getBankDotList(extValueJson.getStr("instBranchName"));
//                            if ("200".equals(instBranchName.getStr("code"))){
//                                JSONArray jsonArray1 = instBranchName.getJSONObject("data").getJSONArray("recordList");
//                                if (!jsonArray1.isEmpty()){
//                                    //fundPaymentBItem.put("oppositebankaddr",jsonArray1.getJSONObject(0).getStr("id")); //收款方开户行名称
//                                    fundPaymentBItem.put("oppositebanklineno",jsonArray1.getJSONObject(0).getStr("linenumber")); //收款方银行联行号
//                                    fundPaymentBItem.put("oppositebankType",jsonArray1.getJSONObject(0).getStr("bank_name")); //收款方银行类别
//
//                                    bankParamMap.put("openaccountbank",jsonArray1.getJSONObject(0).getStr("id")); //银行网点id
//                                    bankParamMap.put("bank",jsonArray1.getJSONObject(0).getStr("bank")); //银行类型id
//                                }else{
//                                    throw new RuntimeException("{error:\"获取付款单银行网点编码失败-资金调拨审批单"+businessId+"中银行网点编码不存在！！！\"}");
//                                }
//                            }
//                            //logger.info("收款公司银行账号:{}",cardNo);
//                            //获取供应商编码
//                            String vendorCode = defaultDataService.getVendorCode(extValueJson.getStr("name"), bankParamMap, vendorList);
//                            if (vendorCode == null){
//                                throw new RuntimeException("{error:\"获取付款单供应商编码失败-资金调拨审批单"+businessId+"中供应商编码不存在！！！\"}");
//                            }
//                            fundPaymentBItem.put("oppositeobjectname", vendorCode); // 付款对象(供应商)
//                            fundPaymentBItem.put("oppositeaccountname",name); //收款账户名称
//                            logger.info("收款公司供应商编码{}",vendorCode);
//                            fundPaymentBItem.put("oppositeaccountno", cardNo); // 收款账号
//                        }
//                    }
//                }
                    //转入公司银行账号(收款账号)
                    if ("RecipientAccountField-JVM0OK11".equals(item.getStr("id"))){
//                        //设置收款方单位，收款方账号
                        if (item.containsKey("extValue")){
                            JSONObject extValueJson = item.getJSONObject("extValue");
                            if (!extValueJson.containsKey("cardNo")) {
                                throw new RuntimeException("{error:\"获取付款单供应商银行账号失败-资金调拨审批单" + businessId + "中供应商收款账号不存在！！！\"}");
                            }
                            String name = extValueJson.getStr("name");
                            //供应商子表银行网点信息参数
                            Map<String,Object> bankParamMap = new HashMap<>();
                            String cardNo = extValueJson.getStr("cardNo"); //银行账号
                            bankParamMap.put("account",extValueJson.getStr("cardNo")); //银行账号
                            bankParamMap.put("accountname",extValueJson.getStr("name")); ////账号名称

                            JSONObject instBranchName = ysDataGetService.getBankDotList(extValueJson.getStr("instBranchName"));
                            if ("200".equals(instBranchName.getStr("code"))){
                                JSONArray jsonArray1 = instBranchName.getJSONObject("data").getJSONArray("recordList");
                                if (!jsonArray1.isEmpty()){
                                    //fundPaymentBItem.put("oppositebankaddr",jsonArray1.getJSONObject(0).getStr("id")); //收款方开户行名称
                                    fundPaymentBItem.put("oppositebanklineno",jsonArray1.getJSONObject(0).getStr("linenumber")); //收款方银行联行号
                                    fundPaymentBItem.put("oppositebankType",jsonArray1.getJSONObject(0).getStr("bank_name")); //收款方银行类别

                                    bankParamMap.put("openaccountbank",jsonArray1.getJSONObject(0).getStr("id")); //银行网点id
                                    bankParamMap.put("bank",jsonArray1.getJSONObject(0).getStr("bank")); //银行类型id
                                }else{
                                    throw new RuntimeException("{error:\"获取付款单银行网点编码失败-资金调拨审批单"+businessId+"中银行网点编码不存在！！！\"}");
                                }
                            }
                            //logger.info("收款公司银行账号:{}",cardNo);
                            //获取供应商编码
                            String vendorCode = defaultDataService.getVendorCode(extValueJson.getStr("name"), bankParamMap, vendorList);
                            if (vendorCode == null){
                                throw new RuntimeException("{error:\"获取付款单供应商编码失败-资金调拨审批单"+businessId+"中供应商编码不存在！！！\"}");
                            }
                            fundPaymentBItem.put("oppositeobjectname", vendorCode); // 付款对象(供应商)
                            fundPaymentBItem.put("oppositeaccountname",name); //收款账户名称
                            logger.info("收款公司供应商编码{}",vendorCode);
                            fundPaymentBItem.put("oppositeaccountno", cardNo); // 收款账号
                        }
                    }
                }
                fundPaymentBList.add(fundPaymentBItem);
                paramMap.put("FundPayment_b", fundPaymentBList); //将子表放入到主表中
                dataMap.put("data", paramMap); //最终封装的数据
                //发送请求保存资金付款单
                jsonObjectResponse = defaultDataService.sendPostToSaveFundPayment(dataMap);

                defaultDataService.processResponseResults(jsonObjectResponse,businessId,"fundPayment",processInstance);
                ysJsonResponseList.add(jsonObjectResponse.getJSONObject("data"));
                //ysJsonResponseList.add(new JSONObject(dataMap));
            }catch (Exception e){
                //回显错误信息
                //e.printStackTrace();
                ysJsonResponseList.add(new JSONObject(e.getMessage()));
                //记录xxl-job日志
                defaultDataService.InsertLogger(businessId,"0",e.getMessage(),"panzhinew3");
                logger.error("资金调拨错误信息:{}", e.getMessage());
                logger.info("---------------------------------------------------------------------------");
            }
        }
        return ysJsonResponseList;
    }

    /**
     * 处理资金调拨中收款主体参数保存到ys资金收款单中
     * @param paramMap
     * @param fundPaymentBItem
     * @param jsonObject
     * @return
     */
    public JSONObject fundAllocation_fundCollectionBill(Map<String,Object> paramMap,Map<String,Object> fundPaymentBItem,JSONObject jsonObject,List<JSONObject> bankList,List<JSONObject> vendorList) throws Exception {
        //参数汇总
        Map<String, Object> dataMap = new HashMap<>();
        //构建资金收款单主表
        Map<String, Object> fundCollectionBillMap = new HashMap<>(paramMap);
        //构建资金收款单子表
        List<Map<String, Object>> fundPaymentBList = new ArrayList<>();
        String businessId = jsonObject.getJSONObject("result").getStr("businessId");
        JSONArray jsonArray = jsonObject.getJSONObject("result").getJSONArray("formComponentValues");

        //修改交易类型
        fundCollectionBillMap.put("tradetype_code", defaultDataService.TRADE_TYPE_CODE[3]);  //收款单交易类型
        for(int i=0;i<jsonArray.size();i++){
            JSONObject item = jsonArray.getJSONObject(i);
            //设置金额
            if ("TableField-MINGXI".equals(item.getStr("id"))){
                JSONArray value = item.getJSONArray("value");
                JSONArray rowValue = value.getJSONObject(0).getJSONArray("rowValue");
                //检查该数据中是否包含金额value
                if ("调拨金额（元）".equals(rowValue.getJSONObject(1).getStr("label"))){
                    double amount = Double.parseDouble(rowValue.getJSONObject(1).getStr("value"));
                    fundPaymentBItem.put("oriSum",amount);
                }
                continue;
            }
            if ("DDSelectField_326QK5S77MW0".equals(item.getStr("id"))){
                String value = item.getStr("value");
                fundPaymentBItem.put("quickType_code",quickTypeMap.get(value));
                continue;
            }
            //设置收款公司主体(收款单参数)
            if("RecipientAccountField-JVM0OK11".equals(item.getStr("id"))){
                //缓存到redis中
                String uitCodeKey = "组织名称:" + item.getStr("value");
                String uitCode = (String) RedisUtils.get(uitCodeKey);
                //不存在发送请求获取组织编码
                if (StringUtils.isEmpty(uitCode)){
                    //根据组织名称获取code
                    uitCode = ysDataGetService.getOrgUitCode(item.getStr("value"));
                    RedisUtils.set(uitCodeKey, uitCode);
                }
                if (uitCode!=null && !uitCode.isEmpty()){
                    fundCollectionBillMap.put("accentityRaw_code",uitCode );
                    fundCollectionBillMap.put("accentity_code", uitCode);
                    fundCollectionBillMap.put("org_code", uitCode);
                    //设置收款账户
                    if (item.containsKey("extValue")){
                        JSONObject extValueJson = item.getJSONObject("extValue");
                        if (!extValueJson.containsKey("cardNo")){
                            logger.error("{error:\"获取收款单默认企业银行账号失败-资金调拨审批单"+businessId+"中收款企业默认银行账号不存在！！！\"}");
                            //throw new RuntimeException("{error:\"获取收款单默认企业银行账号失败-资金调拨审批单"+businessId+"中收款企业默认银行账号不存在！！！\"}");
                        }
                        String cardNo = extValueJson.getStr("cardNo");
                        logger.info("收款公司银行账号:{}",cardNo);
                        fundCollectionBillMap.put("enterprisebankaccount_code",cardNo);// 企业银行账户编码
                        fundPaymentBItem.put("enterprisebankaccount_code", cardNo); // (子表)企业银行账户编码
                    }
                }else {
                    throw new RuntimeException("{error:\"获取组织code失败-资金调拨审批单"+businessId+"中收款组织不存在！！！\"}");
                }
                continue;
            }
            //设置收款公司银行账号
            if ("RecipientAccountField-JVM0OK11".equals(item.getStr("id"))){
                continue;
            }

            if ("TextField_1WBABIWCT5FK0".equals(item.getStr("id"))){

                // 查找已有供应商
                Optional<JSONObject> matchedVendorOpt = findMatchingVendor(item.getStr("value"), vendorList);
                if (matchedVendorOpt.isPresent()) {
                    JSONObject vendor = matchedVendorOpt.get();
                    logger.info("找到匹配供应商: {}", vendor.getStr("name"));

                    String vendorId = vendor.getStr("id");
                    JSONObject vendorDetails = ysDataGetService.getVendorBankCode(vendorId);
                    if (vendorDetails == null || vendorDetails.isEmpty()) {
                        logger.warn("供应商详情为空，ID: {}", vendorId);
                        return null;
                    }else{
                        fundPaymentBItem.put("oppositeobjectname",vendorDetails.getStr("code"));
                    }
                }else{
                    throw new RuntimeException("{error:\"获取付款单供应商编码失败-资金调拨审批单"+businessId+"中供应商编码不存在！！！\"}");
                }
                continue;
            }
            //转出公司银行账号(付款账号)
            if ("TextField_UULPWK1H8MO0".equals(item.getStr("id"))){
                //获取企业银行账号
                String bankCode = item.getStr("value");
                if (bankCode!=null && !bankCode.isEmpty()){
                    fundPaymentBItem.put("oppositeaccountno", bankCode); // (子表)企业银行账户编码
                }else {
                    throw new RuntimeException("{error:\"获取企业银行账号失败-资金调拨审批单"+businessId+"中付款银行账号不存在！！！\"}");
                }
                continue;
            }

            //设置付款公司银行账号
//            if ("TextField_9SGHDIG5KI80".equals(item.getStr("id"))){
//                //缓存到redis中
//                String uitCodeKey = "组织名称:" + item.getStr("value");
//                String uitCode = (String) RedisUtils.get(uitCodeKey);
//                //不存在发送请求获取组织编码
//                if (StringUtils.isEmpty(uitCode)){
//                    //根据组织名称获取code
//                    uitCode = ysDataGetService.getOrgUitCode(item.getStr("value"));
//                    RedisUtils.set(uitCodeKey, uitCode);
//                }
//                Map<String,Object> bankParamMap = null;
//                //获取付款对象企业默认银行账号
//                String bankCode = defaultDataService.getBankCode(item.getStr("value"),bankList);
//                for (JSONObject bank : bankList){
//                    if (bank.getStr("code").equals(bankCode)){
//                        String name = item.getStr("value");
//                        fundPaymentBItem.put("oppositeaccountname",name); //收款账户名称
//                        //供应商子表银行信息参数
//                        bankParamMap = new HashMap<>();
//                        bankParamMap.put("account",bankCode); //银行账号
//                        bankParamMap.put("accountname",bank.getStr("accountOrgId_name")); ////账号名称
//
//                        JSONObject instBranchName = ysDataGetService.getBankDotList(bank.getStr("bankNumber_name")); //获取网点信息
//                        if ("200".equals(instBranchName.getStr("code"))){
//                            JSONArray jsonArray1 = instBranchName.getJSONObject("data").getJSONArray("recordList");
//                            if (!jsonArray1.isEmpty()){
//                                //fundPaymentBItem.put("oppositebankaddr",jsonArray1.getJSONObject(0).getStr("id")); //收款方开户行名称
//                                fundPaymentBItem.put("oppositebanklineno",jsonArray1.getJSONObject(0).getStr("linenumber")); //收款方银行联行号
//                                fundPaymentBItem.put("oppositebankType",jsonArray1.getJSONObject(0).getStr("bank_name")); //收款方银行类别名称
//
//                                bankParamMap.put("openaccountbank",jsonArray1.getJSONObject(0).getStr("id")); //银行网点id
//                                bankParamMap.put("bank",jsonArray1.getJSONObject(0).getStr("bank")); //银行类型id
//                                break;
//                            }else{
//                                throw new RuntimeException("{error:\"获取付款单银行网点编码失败-资金调拨审批单"+businessId+"中银行网点编码不存在！！！\"}");
//                            }
//                        }
//                    }
//                }
//                logger.info("付款公司银行账号{}",bankCode);
//                if (bankCode!=null && !bankCode.isEmpty()){
//                    fundPaymentBItem.put("oppositeaccountno", bankCode); //收款账号
//                    //获取供应商编码
//                    String vendorCode = defaultDataService.getVendorCode(item.getStr("value"), bankParamMap, vendorList);
//                    if (vendorCode == null){
//                        throw new RuntimeException("{error:\"获取收款款单供应商编码失败-资金调拨审批单"+businessId+"中供应商编码不存在！！！\"}");
//                    }
////                    else if (vendorCode.isEmpty()){
////                        throw new RuntimeException("{error:\"无需保存该单据-预付款审批单"+businessId+"中供应商为内部单位！！！\"}");
////                    }
//                    fundPaymentBItem.put("oppositeobjectname", vendorCode); // 付款对象(供应商)
//                    logger.info("付款公司供应商编码{}",vendorCode);
//
//                }else {
//                    logger.error("{error:\"获取收款单付款银行账号失败-资金调拨审批单"+businessId+"中付款企业默认银行账号不存在！！！\"}");
//                    //throw new RuntimeException("{error:\"获取收款单付款银行账号失败-资金调拨审批单"+businessId+"中付款企业默认银行账号不存在！！！\"}");
//                }
//            }
        }
        fundPaymentBList.add(fundPaymentBItem);
        fundCollectionBillMap.put("FundCollection_b", fundPaymentBList); //将子表放入到主表中
        dataMap.put("data", fundCollectionBillMap); //最终封装的数据
        //发送请求保存资金收款单
        return defaultDataService.sendPostToSaveFundCollection(dataMap);
        //return new JSONObject(dataMap);
    }

    public static String dealTime(String oldTime){

        OffsetDateTime offsetDateTime = OffsetDateTime.parse(oldTime);
        // 定义目标格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化为指定格式
        String formattedTime = offsetDateTime.format(formatter);

        return formattedTime;
    }

    /**
     * 查找匹配的供应商
     */
    private Optional<JSONObject> findMatchingVendor(String vendorName, List<JSONObject> vendorList) {
        return vendorList.stream()
                .filter(obj -> obj.getStr("name").contains(vendorName))
                .findFirst();
    }


}
